package com.hsyt.pms.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pfc.support.util.EnumToDataUtil;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.PltAuth;
import com.hsyt.pms.api.common.PltLog;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.system.SysDictionaryDto;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.enums.DictionaryGroup;
import com.hsyt.pms.api.enums.Status;
import com.hsyt.pms.api.vo.system.SysDictionaryVo;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.sys.dubbo.SysDictionaryService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.Date;

/**
 * @ClassName: SysDictController
 * @Description: 数据字典服务控制层
 * @author: wangze@hsyuntai.com
 * @date: 2019/1/29 18:19
 */
@RestController
@RequestMapping("/pms/system/dict")
public class SysDictController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysDictController.class);

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysDictionaryService sysDictionaryService;

    @RequestMapping(value = "/page")
    @PltAuth(auth ="ROLE_DICTIONARY")
    public ModelAndView page(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("system/pageDict", model);
    }

    @RequestMapping(value = "/toEditForm")
    @PltAuth(auth ="ROLE_DICTIONARY_ADD,ROLE_DICTIONARY_MODIFY")
    public ModelAndView toEditForm(ModelMap model, @RequestParam("formType") String formType) {
        model.put("formType", formType);
        return new ModelAndView("system/formDict");
    }

    @RequestMapping(value = "/toEditGroupForm")
    @PltAuth(auth ="ROLE_DICTIONARY_ADD,ROLE_DICTIONARY_MODIFY")
    public ModelAndView toEditGroupForm(ModelMap model, @RequestParam("formType") String formType) {
        model.put("formType", formType);
        return new ModelAndView("system/formGroup");
    }

    @RequestMapping(value = "/saveGroup")
    @PltAuth(auth ="ROLE_DICTIONARY_ADD,ROLE_DICTIONARY_MODIFY")
    public @ResponseBody
    ResultMessage saveGroup(@RequestBody SysDictionaryDto dto, String formType, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            if (dto.getId() == null) {
                dto.setGroupCode(DictionaryGroup.DICTIONARY_GROUP.getCode());
                dto.setGroupName(DictionaryGroup.DICTIONARY_GROUP.getName());
                dto.setGmtCreate(new Date());
                dto.setGmtModify(new Date());
                sysDictionaryService.insertSysDict(dto);
            } else {
                sysDictionaryService.updateByPrimaryKey(dto);

            }
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysParamController-save error: {0}", e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/saveDict")
    @PltAuth(auth ="ROLE_DICTIONARY_ADD,ROLE_DICTIONARY_MODIFY")
    public @ResponseBody
    ResultMessage saveDict(@RequestBody SysDictionaryDto dto, String formType, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            if (dto.getId() == null) {
                dto.setGmtCreate(new Date());
                dto.setGmtModify(new Date());
                sysDictionaryService.insertSysDict(dto);
            } else {
                sysDictionaryService.updateByPrimaryKey(dto);

            }
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysParamController-save error: {0}", e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/editGroupPage")
    @PltAuth(auth ="ROLE_DICTIONARY_ADD,ROLE_DICTIONARY_MODIFY")
    public ModelAndView editGroupPage(ModelMap model, String type) {
        model.put("type", type);
        return new ModelAndView("system/win/editDictGroup", model);
    }

    @PltLog(log = "字典管理分组查询")
    @RequestMapping(value = "/findPageDataGroup")
    public @ResponseBody
    PageResult findPageDataGroup(@RequestBody SysDictionaryDto dto) {
        PageResult<SysDictionaryVo> pageResult = new PageResult<SysDictionaryVo>();
        try {
            pageResult = sysDictionaryService.searchDictGroupPageByDto(dto);
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("SysDictController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(LOGGER, message);
        }
        return pageResult;
    }

    @PltLog(log = "字典管理分页查询")
    @RequestMapping(value = "/findPageDataDict")
    public @ResponseBody
    PageResult findPageDataDict(@RequestBody SysDictionaryDto dto) {
        PageResult<SysDictionaryVo> pageResult = new PageResult<SysDictionaryVo>();
        try {
            if(StringUtils.isBlank(dto.getDictCode()) && StringUtils.isBlank(dto.getDictName()) && StringUtils.isBlank(dto.getGroupCode()) && StringUtils.isBlank(dto.getGroupName())) {
                return pageResult;
            } else {
                pageResult = sysDictionaryService.searchPageDataByDto(dto);
            }
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("SysDictController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(LOGGER, message);
        }
        return pageResult;
    }

    @PltLog(log = "字典管理分页查询")
    @RequestMapping(value = "/findPageData")
    public @ResponseBody
    PageResult findPageData(@RequestBody SysDictionaryDto dto) {
        PageResult<SysDictionaryVo> pageResult = new PageResult<SysDictionaryVo>();
        try {
            pageResult = sysDictionaryService.searchPageDataByDto(dto);
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("SysDictController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(LOGGER, message);
        }
        return pageResult;
    }

    @PltLog(log = "增加字典操作")
    @PltAuth(auth ="ROLE_DICTIONARY_ADD")
    @RequestMapping(value = "/addSysDict")
    public @ResponseBody
    ResultMessage addSysDict(@RequestBody SysDictionaryDto sysDictionaryDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "新增成功");
        try {
            SysDictionaryVo sysDictionary = sysDictionaryService.getDictionary(sysDictionaryDto);
            if (sysDictionary != null && AbleStatus.ENABLED.getCode().equals(sysDictionary.getStatus())) {
                result.change(false, "已存在,请修改枚举编码");
                return result;
            }
            sysDictionaryDto.setGmtCreate(new Date());
            sysDictionaryDto.setGmtModify(new Date());
            sysDictionaryDto.setStatus(AbleStatus.ENABLED.getCode());
            if (sysDictionary != null && AbleStatus.DISABLED.getCode().equals(sysDictionary.getStatus())) {
                sysDictionaryDto.setId(sysDictionary.getId());
                sysDictionaryService.updateByPrimaryKey(sysDictionaryDto);
            } else {
                sysDictionaryService.insertSysDict(sysDictionaryDto);
            }
        } catch (Exception e) {
            result.change(false, "新增失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-addSysDict error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "增加字典分组")
    @PltAuth(auth ="ROLE_DICTIONARY_ADD")
    @RequestMapping(value = "/addSysDictGroup")
    public @ResponseBody
    ResultMessage addSysDictGroup(@RequestBody SysDictionaryDto sysDictionaryDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "新增成功");
        sysDictionaryDto.setId(null);
        try {
            sysDictionaryDto.setGroupCode(DictionaryGroup.DICTIONARY_GROUP.getCode());
            sysDictionaryDto.setGroupName(DictionaryGroup.DICTIONARY_GROUP.getName());
            SysDictionaryVo sysDictionary = sysDictionaryService.getDictionary(sysDictionaryDto);
            if (sysDictionary != null && AbleStatus.DISABLED.equals(sysDictionary.getStatus())) {
                result.change(false, "已存在,请修改字典编码！");
                return result;
            }
            sysDictionaryDto.setGmtCreate(new Date());
            sysDictionaryDto.setGmtModify(new Date());
            sysDictionaryDto.setStatus(AbleStatus.ENABLED.getCode());
            if (sysDictionary != null && AbleStatus.DISABLED.equals(sysDictionary.getStatus())) {
                sysDictionaryDto.setId(sysDictionary.getId());
                sysDictionaryService.updateByPrimaryKey(sysDictionaryDto);
            } else {
                sysDictionaryService.insertSysDict(sysDictionaryDto);
            }
        } catch (Exception e) {
            result.change(false, "新增失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-addSysDictGroup error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "启用/停用字典")
    @PltAuth(auth ="ROLE_PARAM_MG_OPT")
    @RequestMapping(value = "/doUndo")
    public @ResponseBody
    ResultMessage doUndo(@RequestBody SysDictionaryDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "操作成功");
        try {
            dto.setGmtModify(new Date());
            this.sysDictionaryService.updateByPrimaryKey(dto);
        } catch (Exception e) {
            result.change(false, "操作失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysDictController-doUndo error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }


}
